<?php

namespace App\Listeners\Collect;

use App\Consts\GlobalCounterConst;
use App\Events\Collect\CreateCollectEvent;
use App\Listeners\BaseListener;

class CreateCollectListener extends BaseListener
{
    /**
     * Handle the event.
     *
     * @param CreateCollectEvent $event
     * @return bool
     */
    public function handle(CreateCollectEvent $event)
    {
        $this->saveFileLog(__CLASS__, $event);

        $collect = $event->collect;

        // 更新收藏夹内容总数<total> +1
        service()->PackageService->changeTotal([
            'user_id' => $collect['user_id'],
            'package_id' => $collect['package_id'],
            'trend' => GlobalCounterConst::INCREASE
        ]);

        // 队列记录日志
        service()->LogCountService->createLogByQueue($event->logCountLoadData);

        service()->CollectService->addSearchCollect([
            'user_id' => $collect['user_id'],
            'package_id' => $collect['package_id'],
            'target_id' => $collect['target_id'],
            'target_type' => $collect['target_type'],
        ]);
        return true;
    }
}
